/* @(#)GSManufactureScanQueryAction.java    
*
* Project: GS
*
* Modify Information:
* =============================================================================
*   Author       Date       Description
*   ------------ ---------- ---------------------------------------------------
*   Jenny Li     2006-2-13    新增
*   Simonyi Peng 2006-3-18    修改providername->manufacturername
*
* Copyright Notice:
* =============================================================================
*    Copyright (c) 2001-2006 Beijing HiTRUST Technology Co., Ltd. 1808 Room, 
*        Science & Technology Building, No. 9 South Street, Zhong Guan Cun, 
*        Haidian District, Beijing ,100081, China All rights reserved.
*
*    This software is the confidential and proprietary information of
*        Beijing HiTRUST Technology Co., Ltd. ("Confidential Information").
*        You shall not disclose such Confidential Information and shall use it
*        only in accordance with the terms of the license agreement you entered
*        into with HiTRUST.
*
* Warning:
* =============================================================================
* 
*/
package com.hitrust.gs.action;

import java.util.ArrayList;

import system.LoginContext;

import com.hitrust.gs.business.GSManufactureProcessor;
import com.hitrust.gs.business.GSUniteGwProcessor;
import com.hitrust.gs.form.GSManufactureScanQueryForm;
import com.hitrust.gs.lib.GSBaseConstants;
import com.hitrust.gs.lib.GSLogWriter;
import com.hitrust.gs.service.GSBaseAction;
import com.hitrust.gs.service.GSBaseForm;
import com.hitrust.gs.service.GSBaseManager;
import com.hitrust.gs.util.exception.GSException;
import com.hitrust.gs.util.page.ResultProcess;
import com.hitrust.gs.vo.GSManufactureVO;

import framework.core.DateUtil;

/**
* 产品浏览查询操作
*/
public class GSOrderManufactureScanQueryAction extends GSBaseAction {

   public String execute(GSBaseForm aForm) throws Exception {
       
       GSManufactureScanQueryForm tForm = (GSManufactureScanQueryForm)aForm;
       GSManufactureProcessor tGSManufProcessor = new GSManufactureProcessor();
       LoginContext context=(LoginContext) aForm.getSession().getAttribute(GSBaseConstants.LOGIN);
      
       //取得页面参数放入VO对象
       GSManufactureVO tGSManufactureVO = new GSManufactureVO();
       tGSManufactureVO.warehouse_code =  context.getLoginWarehouseCode();
       tGSManufactureVO.gs_concentricity_id = tForm.getConcentricityId();
       tGSManufactureVO.gs_manufacture_name = tForm.getManufactureName();
       tGSManufactureVO.gs_manufacturer_name = tForm.getManufacturerName();
       tGSManufactureVO.gs_manufacture_category1 = tForm.getManuftype1();
       tGSManufactureVO.gs_manufacture_category2 = tForm.getManuftype2();
       tGSManufactureVO.gs_manufacture_category3 = tForm.getManuftype3();
       //tGSManufactureVO.gs_manufacture_price_min = tForm.getManufunitpricemin();
       //tGSManufactureVO.gs_manufacture_price_max = tForm.getManufunitpricemax();
       tGSManufactureVO.gs_manufacture_brand = tForm.getManufactureBrand();
       tGSManufactureVO.gs_manufacture_spec = tForm.getManufactureSpec();
       tGSManufactureVO.gs_manufacture_type = tForm.getManufactureType();
       tGSManufactureVO.gs_balance_mode=tForm.getBalancemode();
       tGSManufactureVO.drawingpiececode=tForm.getDrawingPieceCode();
       tGSManufactureVO.provider_name=tForm.getProvider_name();
       //只能采购自己的临时产品和所有正式产品
       GSUniteGwProcessor unitepro=new GSUniteGwProcessor();
       ArrayList warelist=unitepro.getBzwzAndTmpWareHouse(context.getLoginWarehouseCode());
       tGSManufactureVO.setTmplist(warelist);
       if("T".equals(aForm.getRequest().getParameter("fanye"))){
           tGSManufactureVO = (GSManufactureVO)aForm.getSession().getAttribute("QUERYCONDITION");
       }else{
           aForm.getSession().setAttribute("QUERYCONDITION",tGSManufactureVO);
       }
       String keyword = tForm.getKeyword();
       String iskeywordquery = aForm.getRequest().getParameter("iskeywordquery");
       String isupdate = aForm.getRequest().getParameter("isupdate");
       aForm.getRequest().setAttribute("iskeywordquery",iskeywordquery);

      
       try{
           //根据页面传入参数取得产品信息
           ArrayList tList = null;
           String[] tStr = this.getPageParams(aForm,tGSManufactureVO,keyword,iskeywordquery,warelist);
           if("F".equals(iskeywordquery))
               tList = tGSManufProcessor.getManufactureByOpt_(tGSManufactureVO,Integer.parseInt(tStr[0]),Integer.parseInt(tStr[1]));
           else 
               tList = tGSManufProcessor.getManufactureByKeyword(keyword,Integer.parseInt(tStr[0]),Integer.parseInt(tStr[1]),warelist);
           //放入ResultProcess对象为页面分页做准备
           ResultProcess tProc = new ResultProcess();
           tProc.setResult(tList);
           tForm.getRequest().setAttribute("manufacture",tProc);
           //存储分页参数
           tForm.getRequest().setAttribute("pageSize",tStr[1]);
           tForm.getRequest().setAttribute("totalRecords",tStr[2]);
           
           //暂时用此方法传递分页参数
           //tForm.getRequest().setAttribute("choice",tForm.getRequest().getParameter("choice"));
           //查询产品等级
           GSManufactureProcessor mproc = new GSManufactureProcessor();
           mproc.getManufactureOfLevel(aForm);

       }catch(GSException e){
           tForm.getRequest().setAttribute("action","A100105.do");
           throw e;
       }catch(Exception e){
           GSLogWriter.error(this,e.toString());
           tForm.getRequest().setAttribute("action","A100105.do");
           aForm.getRequest().setAttribute("window","true");
           throw new GSException(GSException.E10030,GSException.E10030_MSG);
       }
       
       if("T".equals(isupdate)){
           return "success";
           
       }else{
           return GSBaseConstants.FORWARD_SUCCESS;
       }
   }

   /**
    * 取得分页参数
    * @param aForm         基类form
    * @param aOrderVO      订单VO
    * @param aOrg          组织ArrayList对象
    * @return
    * @throws Exception
    */
   private String[] getPageParams(GSBaseForm aForm,GSManufactureVO tGSManufactureVO,String keyword,String iskeywordquery,ArrayList tmplist) throws Exception{
       String tStr[] = new String[3];
       int totalRecords;
       int pageNo;
       int pageSize;
       String tPage = GSBaseConstants.tPage;
       GSManufactureProcessor tProc = new GSManufactureProcessor();
       //根据页面传入的查询参数查询订单信息
       if(aForm.getChoice() == null || aForm.getChoice().equals("")){
           if("F".equals(iskeywordquery)){                
               totalRecords = tProc.getManufactureCountByOpt_(tGSManufactureVO);
           }
           else{ 
               totalRecords = tProc.getManufactureCountByKeyword(keyword,tmplist);
           }
           
           // 取得系统默认的页大小
           pageSize = Integer.parseInt(tPage);
           // PageNo=1
           pageNo = 1;
           int n = (totalRecords % pageSize == 0 )? totalRecords / pageSize:totalRecords / pageSize + 1;
           if (pageNo>n) {
               pageNo=n;
           }
           tStr[0] = "" + pageNo;
           tStr[1] = "" + pageSize;
           tStr[2] = "" + totalRecords;
       }else{
           tStr = GSBaseManager.getPageNoAndPageSize(aForm);
       }
       return tStr;
   }   
}
